/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import muhnes.sizoo.modelo.Genero;
import muhnes.sizoo.HibernateUtilZoologia;
/**
 *
 * @author Ricardo Orellana
 */
public class GeneroDaoImpl implements GeneroDao{
    
    Session sesion;
    
    public GeneroDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }
    
    @Override
    public void guardar( Genero genero ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( genero );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Genero > generos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( generos );
        transaccion.commit();
    }

    @Override
    public void actualizar( Genero genero ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( genero );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Genero > generos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( generos );
        transaccion.commit();
    }

    @Override
    public void eliminar( Genero genero ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( genero );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Genero > generos ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( generos );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Genero genero = buscarPorId( id );
        sesion.delete( genero );
        transaccion.commit();
    }

    @Override
    public Genero buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Genero where CodigoGenero = " + id);
        Genero genero = (Genero)consulta.uniqueResult();
        transaccion.commit();
        return genero;
    }

    @Override
    public List< Genero > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Genero");
        List< Genero > generos = consulta.list();
        transaccion.commit();
        return generos;
    }
    
}
